
Imports System.IO

Namespace binaryReaderHelper
    Public Module binaryReaderHelper
        Public Function readUInt64(ByVal reader As BinaryReader, ByRef rtn As UInt64 _
                                   , Optional ByVal needmsg As Boolean = False) As Boolean
            Try
                rtn = reader.ReadUInt64()
                Return True
            Catch ex As Exception
                If needmsg Then
                    raiseError("cannot read uint64 from BinaryReader, " + ex.Message, errorHandle.errorType.system)
                End If
                Return False
            End Try
        End Function

        Public Function readString(ByVal reader As BinaryReader, ByRef s As String, _
                                   Optional ByVal needmsg As Boolean = False, _
                                   Optional ByVal retryTimes As Int64 = 128) As Boolean
            Dim pos As Int64
            If reader.BaseStream.CanSeek Then
                pos = reader.BaseStream.Position
            Else
                pos = npos
            End If
            While retryTimes > 0
                Try
                    s = reader.ReadString()
                    Return True
                Catch ex As IndexOutOfRangeException
                    retryTimes -= 1
                Catch ex As EndOfStreamException
                    Return False
                Catch ex As Exception
                    If needmsg Then
                        raiseError("cannot read string from BinaryReader, " + ex.Message, errorHandle.errorType.system)
                    End If
                    retryTimes -= 1
                End Try

                If pos <> npos Then
                    reader.BaseStream.Position = pos
                End If
            End While

            Return False
        End Function

        Public Function readInt64(ByVal reader As BinaryReader, ByRef rtn As Int64 _
                                  , Optional ByVal needmsg As Boolean = False) As Boolean
            Try
                rtn = reader.ReadInt64()
                Return True
            Catch ex As Exception
                If needmsg Then
                    raiseError("cannot read int64 from BinaryReader, " + ex.Message, errorHandle.errorType.system)
                End If
                Return False
            End Try
        End Function

        Public Function readInt32(ByVal reader As BinaryReader, ByRef rtn As Int32, _
                                  Optional ByVal needmsg As Boolean = False) As Boolean
            Try
                rtn = reader.ReadInt32()
                Return True
            Catch ex As Exception
                If needmsg Then
                    raiseError("cannot read int64 from BinaryReader, " + ex.Message, errorHandle.errorType.system)
                End If
                Return False
            End Try
        End Function
    End Module
End Namespace
